package com.leetcode.hash.demo2;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author Zan
 * @Create 2024/5/16 8:47
 * @ClassName: Test
 * @Description : https://leetcode.cn/problems/intersection-of-two-arrays/description/  两个数组的交集
 */
public class Test {
    public static void main(String[] args) {

    }
}

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> sets = new HashSet<>();
        Set<Integer> results = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            sets.add(nums1[i]);
        }
        for (int i = 0; i < nums2.length; i++) {
            if (sets.contains(nums2[i])) {
                results.add(nums2[i]);
            }
        }
        int[] arr = new int[results.size()];
        int i = 0;
        for (Integer result : results) {
            arr[i++] = result;
        }
        return arr;
    }
}